#include "changeqmlcolor.h"
#include <QDateTime>
#include <QColor>
#include <QVariant>
#include<QtDebug>
ChangeQmlColor::ChangeQmlColor(QObject *target, QObject *parent)
    : QObject(parent)
    , m_timer(this)
    , m_target(target)
{
    qsrand(QDateTime::currentDateTime().toTime_t());
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
    m_timer.start(1000);
}

ChangeQmlColor::~ChangeQmlColor()
{

}

void ChangeQmlColor::onTimeout()
{
//    int  width=qrand()*m_target->property("width").toInt();//QML退出按钮宽度改变
//    m_target->setProperty("width",width);
    QObject *quitButtonText=m_target->findChild<QObject*>("quitButtonText");
    if(quitButtonText)
    {
       QColor color = QColor::fromRgb(qrand()%256, qrand()%256, qrand()%256);//QML主界面退出按钮文本改变
        quitButtonText->setProperty("color",color);
    }

    QColor color = QColor::fromRgb(qrand()%256, qrand()%256, qrand()%256);//QML主界面颜色改变
    m_target->parent()->setProperty("color", color);

    //调用QML函数方法
//    QVariant tre,tre1="ChangeQmlColor From C++";
//    QMetaObject::invokeMethod(m_target->parent(),"myQmlFunction2",Q_RETURN_ARG(QVariant,tre),Q_ARG(QVariant,tre1));
//    qDebug()<<"QML function  myQmlFunction2:"<<tre.toString();
}

